package CtrLayer;

import java.util.ArrayList;

import ModelLayer.*;
import DBLayer.*;

public class CtrSupplier {
	
	public CtrSupplier() {
		
	}
	
	public int updateSupplier(String name, String address, String country, int phoneNo, String email)
    {
		IFDbSupplier dbSuppliers = new DbSupplier();
		Supplier supplier = new Supplier();
        supplier.setName(name);
        supplier.setAddress(address);
        supplier.setCountry(country);
        supplier.setEmail(email);
        supplier.setPhoneNo(phoneNo);
        return  dbSuppliers.updateSupplier(supplier);
    }
	
	public void insertNew(String name, String address, String country, int phoneNo, String email)
    {    
		Supplier supObj = new Supplier();
		supObj.setName(name);
		supObj.setAddress(address);
		supObj.setCountry(country);
		supObj.setEmail(email);
		supObj.setPhoneNo(phoneNo);
  
		try{
			DbConnection.startTransaction();
			DbSupplier dbSup = new DbSupplier();
			dbSup.insertSupplier(supObj);
			DbConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DbConnection.rollbackTransaction();
		}
		
		
    }
	public ArrayList<Supplier> findAllSuppliers() {
		IFDbSupplier dbSuppliers = new DbSupplier();
	    ArrayList<Supplier> allSuppliers = new ArrayList<Supplier>();
	    allSuppliers = dbSuppliers.getAllSuppliers(false);
	    return allSuppliers;
	}
	
	public Supplier findByName(String name) {
		IFDbSupplier dbSuppliers = new DbSupplier();
		return dbSuppliers.searchSupplierName(name, true);
	}
	
	public Supplier findById(int id) {
		IFDbSupplier dbSuppliers = new DbSupplier();
		return dbSuppliers.searchSupplierId(id, true);

	}
	
	public void deleteSupplier(int supplierId) {
		IFDbSupplier dbSuppliers=new DbSupplier();
		dbSuppliers.deleteSupplier(supplierId);
	}

}
